﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Chefbook.ViewModels;
using Chefbook.DataObjects;
using Chefbook.HelperMethods;

namespace Chefbook.Views
{
	/// <summary>
	/// Interaction logic for RecipeEditView.xaml
	/// </summary>
	public partial class RecipeEditView
	{
        /// <summary>
        /// Edit View of a recipe
        /// </summary>
		public RecipeEditView()
		{
			this.InitializeComponent();
            this.InputBindings.Add(new InputBinding(ApplicationCommands.Undo, new KeyGesture(Key.Z, ModifierKeys.Control)));
            this.InputBindings.Add(new InputBinding(ApplicationCommands.Redo, new KeyGesture(Key.Y, ModifierKeys.Control)));
            this.AddHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(ExecutedHandler), true); // we need to see all events to subvert the built-in undo/redo tracking in the text boxes
		}

        //public void CanUndoHandler(object parameter, CanExecuteRoutedEventArgs args) { args.CanExecute = true; }
        //public void CanRedoHandler(object parameter, CanExecuteRoutedEventArgs args) { args.CanExecute = true; }
        //public void UndoHandler(object parameter, ExecutedRoutedEventArgs args) { System.Windows.MessageBox.Show("Undo"); }
        //public void RedoHandler(object parameter, ExecutedRoutedEventArgs args) { System.Windows.MessageBox.Show("Redo"); }

        private void ExecutedHandler(object sender, ExecutedRoutedEventArgs e)
        {
            var textBox = Keyboard.FocusedElement as TextBox;
            if (e.Command == ApplicationCommands.Undo)
            {
                e.Handled = true;
                //if (textBox != null && textBox.Undo())
                //{ return; }
                ((RecipeViewModel)DataContext).Undo();
                //UndoHandler(sender, e);
            }

            if (e.Command == ApplicationCommands.Redo)
            {
                e.Handled = true;
                //if (textBox != null && textBox.Redo())
                //{ return; }
                ((RecipeViewModel)DataContext).Redo();
                //RedoHandler(sender, e);
            }
        }
	}
}